package logic

import (
	"context"
	"errors"
	"video-guard/models/mysql"

	"video-guard/videorpc/internal/svc"
	"video-guard/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type VideoAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVideoAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VideoAddLogic {
	return &VideoAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *VideoAddLogic) VideoAdd(in *videorpc.VideoAddRequest) (*videorpc.VideoAddResponse, error) {
	// todo: add your logic here and delete this line

	var video mysql.VideoInfo

	err := video.FindVideoByTitle(mysql.DB, in.VideoTitle)
	if err != nil {
		return nil, errors.New("查询失败")
	}

	if video.ID != 0 {
		return nil, errors.New("该视频已存在")
	}

	video = mysql.VideoInfo{
		UploadUserId:  in.UploadUserId,
		VideoTitle:    in.VideoCategory,
		VideoCategory: in.VideoCategory,
		VideoLink:     in.VideoLink,
		CoverLink:     in.CoverLink,
		VideoIntro:    in.VideoIntro,
		TopicId:       int(in.TopicId),
	}

	err = video.VideoAdd(mysql.DB)
	if err != nil {
		return nil, errors.New("添加失败")
	}

	return &videorpc.VideoAddResponse{
		VideoId: int64(video.ID),
	}, nil
}

//{
//"uploadUserId": 1,
//"videoTitle": "标题4",
//"videoCategory": "分类1",
//"videoLink": "视频URL4",
//"coverLink": "封面URL4",
//"videoIntro": "简介4",
//"topicId": 2
//}
